//
//  BA_MediaBannerView.m
//  BaseProject
//
//  Created by 王洋 on 2021/2/1.
//  Copyright © 2021 王洋. All rights reserved.
//


#import "BA_MediaBannerView.h"

@interface BA_MediaBannerView ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) TT_AVPlayer *avPlayer;

@property (nonatomic, strong) SDCycleScrollView *bannerView;

@property (nonatomic, strong) MASConstraint *playerWidthConstraint;
@end

@implementation BA_MediaBannerView

- (void)dealloc {
    [self.avPlayer pause];
}

- (void)pause {
    [self.avPlayer pause];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initialization];
        [self setupMainView];
    }
    return self;
}

- (void)fillWithImgArray:(NSMutableArray *)imgArray AndVideoUrl:(NSString *)videoUrl {
    
    self.bannerView.imageURLStringsGroup = imgArray.mutableCopy;
    [self.avPlayer initPlayerWithUrlString:videoUrl AndMaskUrl:imgArray.firstObject];

    if (videoUrl.length > 0) {
        self.contentSize = CGSizeMake(2 * TTScreenWidth, 0);
        self.playerWidthConstraint.mas_equalTo(TTScreenWidth);
        self.bannerView.infiniteLoop = NO;
        self.bannerView.autoScroll = NO;
    } else {
        self.contentSize = CGSizeMake(TTScreenWidth, 0);
        self.playerWidthConstraint.mas_equalTo(0);
        self.bannerView.infiniteLoop = YES;
        self.bannerView.autoScroll = NO;
    }
}

- (void)fillWithImgArray:(NSMutableArray *)imgArray videoUrl:(NSString *)videoUrl andMaskUrl:(NSString *)maskUrl {
    
    self.bannerView.imageURLStringsGroup = imgArray.mutableCopy;
    [self.avPlayer initPlayerWithUrlString:videoUrl AndMaskUrl:maskUrl];

    if (videoUrl.length > 0) {
        self.contentSize = CGSizeMake(2 * TTScreenWidth, 0);
        self.playerWidthConstraint.mas_equalTo(TTScreenWidth);
        self.bannerView.infiniteLoop = NO;
        self.bannerView.autoScroll = NO;
    } else {
        self.contentSize = CGSizeMake(TTScreenWidth, 0);
        self.playerWidthConstraint.mas_equalTo(0);
        self.bannerView.infiniteLoop = YES;
        self.bannerView.autoScroll = NO;
    }
}

- (void)initialization {
    
    self.backgroundColor = UIColorHex(0xf1f1f1);
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.pagingEnabled = YES;
    self.bounces = NO;
}

- (void)setupMainView {
    
    [self addSubview:self.avPlayer];
    [self addSubview:self.bannerView];
        
    [self.avPlayer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.height.mas_equalTo(self);
        self.playerWidthConstraint = make.width.mas_equalTo(TTScreenWidth);
    }];

    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.avPlayer.mas_right);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(TTScreenWidth);
        make.height.mas_equalTo(self);
    }];

    [self layoutIfNeeded];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    if (self.bannerIndexClicked) {
        self.bannerIndexClicked(index);
    }
}

- (TT_AVPlayer *)avPlayer {
    if (!_avPlayer) {
        _avPlayer = [[TT_AVPlayer alloc] init];
    }
    return _avPlayer;
}

- (SDCycleScrollView *)bannerView {
    if (!_bannerView) {
        _bannerView = [[SDCycleScrollView alloc] init];
        _bannerView.backgroundColor = UIColorHex(0xf5f5f5);
        _bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _bannerView.placeholderImage = [UIImage imageNamed:@"placeholder"];
        _bannerView.autoScroll = NO;
        _bannerView.infiniteLoop = NO;
        _bannerView.delegate = self;
        _bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _bannerView.currentPageDotColor = [UIColorHex(0xffffff) colorWithAlphaComponent:0.7];
        _bannerView.pageDotColor = [UIColorHex(0x666666) colorWithAlphaComponent:0.5];
    }
    return _bannerView;
}

@end
